import binascii
import json
import socket
import time
import os

import machine
from machine import I2C, Pin, PWM, Timer

from ssd1306 import SSD1306_I2C
import network

from umqtt.simple import MQTTClient
# 云台舵机控制

# 添加TOML支持
import utoml


class ESPTPZControl:
    def __init__(self, ptz_head_pin, ptz_body_pin):
        try:
            self.ptz_head_pin = PWM(Pin(ptz_head_pin, Pin.OUT))
            self.ptz_body_pin = PWM(Pin(ptz_body_pin, Pin.OUT))
            self.ptz_head_pin.freq(50)
            self.ptz_body_pin.freq(50)
        except Exception as e:
            print(f"PTZ初始化失败: {e}")
            raise

    def _calculate_duty(self, angle):
        """计算PWM占空比"""
        return int((angle / 180 * 2 + 0.5) / 20 * 1023)

    def control_head(self, angle):
        """控制头部舵机"""
        if not 0 <= angle <= 180:
            raise ValueError("角度必须在0-180之间")
        self.ptz_head_pin.duty(self._calculate_duty(angle))

    def control_body(self, angle):
        """控制身体舵机"""
        if not 0 <= angle <= 180:
            raise ValueError("角度必须在0-180之间")
        self.ptz_body_pin.duty(self._calculate_duty(angle))

    def __demo__(self):
        esp_tpz_object = ESPTPZControl(13, 14)
        while True:
            esp_tpz_object.control_head(0)
            esp_tpz_object.control_body(0)
            time.sleep(1)
            esp_tpz_object.control_head(90)
            esp_tpz_object.control_body(90)
            time.sleep(1)


class ESPScreenControl:
    def __init__(self, scl_pin, sda_pin, screen_width=128, screen_height=64):
        try:
            i2c = I2C(scl=Pin(scl_pin), sda=Pin(sda_pin), freq=10000)
            self._screen = SSD1306_I2C(screen_width, screen_height, i2c)
            self._width = screen_width
            self._height = screen_height
        except Exception as e:
            print(f"屏幕初始化失败: {e}")
            raise

    def _clear_message_from_screen(self):
        """清空屏幕函数"""
        self._screen.fill(0)  # 将整个屏幕填充为黑色，相当于清空屏幕
        self._screen.show()

    def show_message_to_screen(self, message):
        print(message)
        self._clear_message_from_screen()
        # 多行显示
        if len(message) < 15:
            self._screen.text(message, 25, 25, 1)
            self._screen.show()
        else:
            segments = [message[i:i + 15] for i in range(0, len(message), 15)]
            tmp_x = 0
            tmp_y = 1
            for index, segment in enumerate(segments):
                if index == 0:
                    pass
                else:
                    tmp_x += 0
                    tmp_y += 10

                self._screen.text(segment, tmp_x, tmp_y, 1)
                self._screen.show()
                # 0.96的文字只能显示5行，如果按10px间距
                if index != 0 and index % 5 == 0:
                    tmp_x = 0
                    tmp_y = 1
                    time.sleep(3)

                    self._clear_message_from_screen()

            # self._screen.show()

    def show_picture_to_screen(self, picture_pixe_list):
        picture_pixe_list = picture_pixe_list
        self._screen.fill(0)
        self._screen.pixel(32, 16, 1)
        for item in iter(picture_pixe_list):
            x = item[0]
            y = item[1]
            self._screen.pixel(x, y, 1)
        self._screen.show()


class ESPPicture:
    # 就是像素点在128*64上的像素点位置
    # 制作的工具在esp_tools.ESPPicturePixe中
    NormalFace = [[32, 16], [33, 16], [34, 16], [35, 16], [36, 16], [84, 16], [85, 16], [86, 16], [87, 16], [88, 16],
                  [29, 17], [30, 17], [31, 17], [32, 17], [33, 17], [34, 17], [35, 17], [36, 17], [37, 17], [38, 17],
                  [39, 17], [82, 17], [83, 17], [84, 17], [85, 17], [86, 17], [87, 17], [88, 17], [89, 17], [90, 17],
                  [91, 17], [28, 18], [29, 18], [30, 18], [31, 18], [32, 18], [33, 18], [34, 18], [35, 18], [36, 18],
                  [37, 18], [38, 18], [39, 18], [40, 18], [80, 18], [81, 18], [82, 18], [83, 18], [84, 18], [85, 18],
                  [86, 18], [87, 18], [88, 18], [89, 18], [90, 18], [91, 18], [92, 18], [27, 19], [28, 19], [29, 19],
                  [30, 19], [31, 19], [32, 19], [33, 19], [34, 19], [35, 19], [36, 19], [37, 19], [38, 19], [39, 19],
                  [40, 19], [41, 19], [79, 19], [80, 19], [81, 19], [82, 19], [83, 19], [84, 19], [85, 19], [86, 19],
                  [87, 19], [88, 19], [89, 19], [90, 19], [91, 19], [92, 19], [93, 19], [27, 20], [28, 20], [29, 20],
                  [30, 20], [31, 20], [32, 20], [33, 20], [34, 20], [35, 20], [36, 20], [37, 20], [38, 20], [39, 20],
                  [40, 20], [41, 20], [79, 20], [80, 20], [81, 20], [82, 20], [83, 20], [84, 20], [85, 20], [86, 20],
                  [87, 20], [88, 20], [89, 20], [90, 20], [91, 20], [92, 20], [93, 20], [27, 21], [28, 21], [29, 21],
                  [30, 21], [31, 21], [32, 21], [33, 21], [34, 21], [35, 21], [36, 21], [37, 21], [38, 21], [39, 21],
                  [40, 21], [41, 21], [79, 21], [80, 21], [81, 21], [82, 21], [83, 21], [84, 21], [85, 21], [86, 21],
                  [87, 21], [88, 21], [89, 21], [90, 21], [91, 21], [92, 21], [93, 21], [27, 22], [28, 22], [29, 22],
                  [30, 22], [31, 22], [32, 22], [33, 22], [34, 22], [35, 22], [36, 22], [37, 22], [38, 22], [39, 22],
                  [40, 22], [41, 22], [79, 22], [80, 22], [81, 22], [82, 22], [83, 22], [84, 22], [85, 22], [86, 22],
                  [87, 22], [88, 22], [89, 22], [90, 22], [91, 22], [92, 22], [93, 22], [28, 23], [29, 23], [30, 23],
                  [31, 23], [32, 23], [33, 23], [34, 23], [35, 23], [36, 23], [37, 23], [38, 23], [39, 23], [40, 23],
                  [80, 23], [81, 23], [82, 23], [83, 23], [84, 23], [85, 23], [86, 23], [87, 23], [88, 23], [89, 23],
                  [90, 23], [91, 23], [92, 23], [29, 24], [30, 24], [31, 24], [32, 24], [33, 24], [34, 24], [35, 24],
                  [36, 24], [37, 24], [38, 24], [39, 24], [82, 24], [83, 24], [84, 24], [85, 24], [86, 24], [87, 24],
                  [88, 24], [89, 24], [90, 24], [91, 24], [32, 25], [33, 25], [34, 25], [35, 25], [36, 25], [84, 25],
                  [85, 25], [86, 25], [87, 25], [88, 25], [28, 44], [29, 44], [30, 44], [31, 44], [32, 44], [33, 44],
                  [34, 44], [35, 44], [36, 44], [37, 44], [38, 44], [39, 44], [40, 44], [41, 44], [42, 44], [43, 44],
                  [44, 44], [45, 44], [46, 44], [47, 44], [48, 44], [49, 44], [50, 44], [51, 44], [52, 44], [53, 44],
                  [54, 44], [55, 44], [56, 44], [57, 44], [58, 44], [59, 44], [60, 44], [61, 44], [62, 44], [63, 44],
                  [64, 44], [65, 44], [66, 44], [67, 44], [68, 44], [69, 44], [70, 44], [71, 44], [72, 44], [73, 44],
                  [74, 44], [75, 44], [76, 44], [77, 44], [78, 44], [79, 44], [80, 44], [81, 44], [82, 44], [83, 44],
                  [84, 44], [85, 44], [86, 44], [87, 44], [88, 44], [89, 44], [90, 44], [91, 44], [92, 44], [93, 44],
                  [94, 44], [95, 44], [96, 44], [97, 44], [98, 44], [99, 44], [100, 44], [101, 44], [102, 44], [27, 45],
                  [28, 45], [29, 45], [30, 45], [31, 45], [32, 45], [33, 45], [34, 45], [35, 45], [36, 45], [37, 45],
                  [38, 45], [39, 45], [40, 45], [41, 45], [42, 45], [43, 45], [44, 45], [45, 45], [46, 45], [47, 45],
                  [48, 45], [49, 45], [50, 45], [51, 45], [52, 45], [53, 45], [54, 45], [55, 45], [56, 45], [57, 45],
                  [58, 45], [59, 45], [60, 45], [61, 45], [62, 45], [63, 45], [64, 45], [65, 45], [66, 45], [67, 45],
                  [68, 45], [69, 45], [70, 45], [71, 45], [72, 45], [73, 45], [74, 45], [75, 45], [76, 45], [77, 45],
                  [78, 45], [79, 45], [80, 45], [81, 45], [82, 45], [83, 45], [84, 45], [85, 45], [86, 45], [87, 45],
                  [88, 45], [89, 45], [90, 45], [91, 45], [92, 45], [93, 45], [94, 45], [95, 45], [96, 45], [97, 45],
                  [98, 45], [99, 45], [100, 45], [101, 45], [102, 45], [103, 45], [27, 46], [28, 46], [29, 46],
                  [30, 46], [31, 46], [32, 46], [33, 46], [34, 46], [35, 46], [36, 46], [37, 46], [38, 46], [39, 46],
                  [40, 46], [41, 46], [42, 46], [43, 46], [44, 46], [45, 46], [46, 46], [47, 46], [48, 46], [49, 46],
                  [50, 46], [51, 46], [52, 46], [53, 46], [54, 46], [55, 46], [56, 46], [57, 46], [58, 46], [59, 46],
                  [60, 46], [61, 46], [62, 46], [63, 46], [64, 46], [65, 46], [66, 46], [67, 46], [68, 46], [69, 46],
                  [70, 46], [71, 46], [72, 46], [73, 46], [74, 46], [75, 46], [76, 46], [77, 46], [78, 46], [79, 46],
                  [80, 46], [81, 46], [82, 46], [83, 46], [84, 46], [85, 46], [86, 46], [87, 46], [88, 46], [89, 46],
                  [90, 46], [91, 46], [92, 46], [93, 46], [94, 46], [95, 46], [96, 46], [97, 46], [98, 46], [99, 46],
                  [100, 46], [101, 46], [102, 46], [103, 46], [27, 47], [28, 47], [29, 47], [30, 47], [31, 47],
                  [32, 47], [33, 47], [34, 47], [35, 47], [36, 47], [37, 47], [38, 47], [39, 47], [40, 47], [41, 47],
                  [42, 47], [43, 47], [44, 47], [45, 47], [46, 47], [47, 47], [48, 47], [49, 47], [50, 47], [51, 47],
                  [52, 47], [53, 47], [54, 47], [55, 47], [56, 47], [57, 47], [58, 47], [59, 47], [60, 47], [61, 47],
                  [62, 47], [63, 47], [64, 47], [65, 47], [66, 47], [67, 47], [68, 47], [69, 47], [70, 47], [71, 47],
                  [72, 47], [73, 47], [74, 47], [75, 47], [76, 47], [77, 47], [78, 47], [79, 47], [80, 47], [81, 47],
                  [82, 47], [83, 47], [84, 47], [85, 47], [86, 47], [87, 47], [88, 47], [89, 47], [90, 47], [91, 47],
                  [92, 47], [93, 47], [94, 47], [95, 47], [96, 47], [97, 47], [98, 47], [99, 47], [100, 47], [101, 47],
                  [102, 47], [103, 47], [28, 48], [29, 48], [30, 48], [31, 48], [32, 48], [33, 48], [34, 48], [35, 48],
                  [36, 48], [37, 48], [38, 48], [39, 48], [40, 48], [41, 48], [42, 48], [43, 48], [44, 48], [45, 48],
                  [46, 48], [47, 48], [48, 48], [49, 48], [50, 48], [51, 48], [52, 48], [53, 48], [54, 48], [55, 48],
                  [56, 48], [57, 48], [58, 48], [59, 48], [60, 48], [61, 48], [62, 48], [63, 48], [64, 48], [65, 48],
                  [66, 48], [67, 48], [68, 48], [69, 48], [70, 48], [71, 48], [72, 48], [73, 48], [74, 48], [75, 48],
                  [76, 48], [77, 48], [78, 48], [79, 48], [80, 48], [81, 48], [82, 48], [83, 48], [84, 48], [85, 48],
                  [86, 48], [87, 48], [88, 48], [89, 48], [90, 48], [91, 48], [92, 48], [93, 48], [94, 48], [95, 48],
                  [96, 48], [97, 48], [98, 48], [99, 48], [100, 48], [101, 48], [102, 48]]
    UpsetFace = [[32, 16], [33, 16], [34, 16], [35, 16], [36, 16], [84, 16], [85, 16], [86, 16], [87, 16], [88, 16],
                 [29, 17], [30, 17], [31, 17], [32, 17], [33, 17], [34, 17], [35, 17], [36, 17], [37, 17], [38, 17],
                 [39, 17], [82, 17], [83, 17], [84, 17], [85, 17], [86, 17], [87, 17], [88, 17], [89, 17], [90, 17],
                 [91, 17], [28, 18], [29, 18], [30, 18], [31, 18], [32, 18], [33, 18], [34, 18], [35, 18], [36, 18],
                 [37, 18], [38, 18], [39, 18], [40, 18], [80, 18], [81, 18], [82, 18], [83, 18], [84, 18], [85, 18],
                 [86, 18], [87, 18], [88, 18], [89, 18], [90, 18], [91, 18], [92, 18], [27, 19], [28, 19], [29, 19],
                 [30, 19], [31, 19], [32, 19], [33, 19], [34, 19], [35, 19], [36, 19], [37, 19], [38, 19], [39, 19],
                 [40, 19], [41, 19], [79, 19], [80, 19], [81, 19], [82, 19], [83, 19], [84, 19], [85, 19], [86, 19],
                 [87, 19], [88, 19], [89, 19], [90, 19], [91, 19], [92, 19], [93, 19], [27, 20], [28, 20], [29, 20],
                 [30, 20], [31, 20], [32, 20], [33, 20], [34, 20], [35, 20], [36, 20], [37, 20], [38, 20], [39, 20],
                 [40, 20], [41, 20], [79, 20], [80, 20], [81, 20], [82, 20], [83, 20], [84, 20], [85, 20], [86, 20],
                 [87, 20], [88, 20], [89, 20], [90, 20], [91, 20], [92, 20], [93, 20], [27, 21], [28, 21], [29, 21],
                 [30, 21], [31, 21], [32, 21], [33, 21], [34, 21], [35, 21], [36, 21], [37, 21], [38, 21], [39, 21],
                 [40, 21], [41, 21], [79, 21], [80, 21], [81, 21], [82, 21], [83, 21], [84, 21], [85, 21], [86, 21],
                 [87, 21], [88, 21], [89, 21], [90, 21], [91, 21], [92, 21], [93, 21], [27, 22], [28, 22], [29, 22],
                 [30, 22], [31, 22], [32, 22], [33, 22], [34, 22], [35, 22], [36, 22], [37, 22], [38, 22], [39, 22],
                 [40, 22], [41, 22], [79, 22], [80, 22], [81, 22], [82, 22], [83, 22], [84, 22], [85, 22], [86, 22],
                 [87, 22], [88, 22], [89, 22], [90, 22], [91, 22], [92, 22], [93, 22], [28, 23], [29, 23], [30, 23],
                 [31, 23], [32, 23], [33, 23], [34, 23], [35, 23], [36, 23], [37, 23], [38, 23], [39, 23], [40, 23],
                 [80, 23], [81, 23], [82, 23], [83, 23], [84, 23], [85, 23], [86, 23], [87, 23], [88, 23], [89, 23],
                 [90, 23], [91, 23], [92, 23], [29, 24], [30, 24], [31, 24], [32, 24], [33, 24], [34, 24], [35, 24],
                 [36, 24], [37, 24], [38, 24], [39, 24], [82, 24], [83, 24], [84, 24], [85, 24], [86, 24], [87, 24],
                 [88, 24], [89, 24], [90, 24], [91, 24], [32, 25], [33, 25], [34, 25], [35, 25], [36, 25], [84, 25],
                 [85, 25], [86, 25], [87, 25], [88, 25], [75, 36], [76, 36], [77, 36], [45, 37], [46, 37], [47, 37],
                 [48, 37], [73, 37], [74, 37], [75, 37], [76, 37], [77, 37], [78, 37], [79, 37], [80, 37], [42, 38],
                 [43, 38], [44, 38], [45, 38], [46, 38], [47, 38], [48, 38], [49, 38], [50, 38], [51, 38], [71, 38],
                 [72, 38], [73, 38], [74, 38], [75, 38], [76, 38], [77, 38], [78, 38], [79, 38], [80, 38], [81, 38],
                 [82, 38], [83, 38], [41, 39], [42, 39], [43, 39], [44, 39], [45, 39], [46, 39], [47, 39], [48, 39],
                 [49, 39], [50, 39], [51, 39], [52, 39], [53, 39], [70, 39], [71, 39], [72, 39], [73, 39], [74, 39],
                 [75, 39], [76, 39], [77, 39], [78, 39], [79, 39], [80, 39], [81, 39], [82, 39], [83, 39], [84, 39],
                 [39, 40], [40, 40], [41, 40], [42, 40], [43, 40], [44, 40], [45, 40], [46, 40], [47, 40], [48, 40],
                 [49, 40], [50, 40], [51, 40], [52, 40], [53, 40], [54, 40], [68, 40], [69, 40], [70, 40], [71, 40],
                 [72, 40], [73, 40], [74, 40], [75, 40], [79, 40], [80, 40], [81, 40], [82, 40], [83, 40], [84, 40],
                 [85, 40], [86, 40], [38, 41], [39, 41], [40, 41], [41, 41], [42, 41], [43, 41], [44, 41], [45, 41],
                 [49, 41], [50, 41], [51, 41], [52, 41], [53, 41], [54, 41], [55, 41], [67, 41], [68, 41], [69, 41],
                 [70, 41], [71, 41], [72, 41], [73, 41], [74, 41], [80, 41], [81, 41], [82, 41], [83, 41], [84, 41],
                 [85, 41], [86, 41], [87, 41], [88, 41], [36, 42], [37, 42], [38, 42], [39, 42], [40, 42], [41, 42],
                 [42, 42], [43, 42], [49, 42], [50, 42], [51, 42], [52, 42], [53, 42], [54, 42], [55, 42], [56, 42],
                 [57, 42], [66, 42], [67, 42], [68, 42], [69, 42], [70, 42], [71, 42], [72, 42], [81, 42], [82, 42],
                 [83, 42], [84, 42], [85, 42], [86, 42], [87, 42], [88, 42], [89, 42], [34, 43], [35, 43], [36, 43],
                 [37, 43], [38, 43], [39, 43], [40, 43], [41, 43], [42, 43], [51, 43], [52, 43], [53, 43], [54, 43],
                 [55, 43], [56, 43], [57, 43], [58, 43], [59, 43], [64, 43], [65, 43], [66, 43], [67, 43], [68, 43],
                 [69, 43], [70, 43], [71, 43], [72, 43], [83, 43], [84, 43], [85, 43], [86, 43], [87, 43], [88, 43],
                 [89, 43], [90, 43], [32, 44], [33, 44], [34, 44], [35, 44], [36, 44], [37, 44], [38, 44], [39, 44],
                 [54, 44], [55, 44], [56, 44], [57, 44], [58, 44], [59, 44], [60, 44], [61, 44], [62, 44], [63, 44],
                 [64, 44], [65, 44], [66, 44], [67, 44], [68, 44], [86, 44], [87, 44], [88, 44], [89, 44], [90, 44],
                 [91, 44], [92, 44], [93, 44], [30, 45], [31, 45], [32, 45], [33, 45], [34, 45], [35, 45], [36, 45],
                 [37, 45], [55, 45], [56, 45], [57, 45], [58, 45], [59, 45], [60, 45], [61, 45], [62, 45], [63, 45],
                 [64, 45], [65, 45], [66, 45], [67, 45], [87, 45], [88, 45], [89, 45], [90, 45], [91, 45], [92, 45],
                 [93, 45], [29, 46], [30, 46], [31, 46], [32, 46], [33, 46], [34, 46], [35, 46], [36, 46], [57, 46],
                 [58, 46], [59, 46], [60, 46], [61, 46], [62, 46], [63, 46], [64, 46], [65, 46], [66, 46], [89, 46],
                 [90, 46], [91, 46], [92, 46], [93, 46], [29, 47], [30, 47], [31, 47], [32, 47], [33, 47], [34, 47],
                 [35, 47], [58, 47], [59, 47], [60, 47], [61, 47], [62, 47], [63, 47], [64, 47], [65, 47], [90, 47],
                 [91, 47], [92, 47], [29, 48], [30, 48], [31, 48], [32, 48], [33, 48], [60, 48], [61, 48], [62, 48],
                 [63, 48], [30, 49], [31, 49]]
    HappyFace = [[32, 16], [33, 16], [34, 16], [35, 16], [36, 16], [84, 16], [85, 16], [86, 16], [87, 16], [88, 16],
                 [29, 17], [30, 17], [31, 17], [32, 17], [33, 17], [34, 17], [35, 17], [36, 17], [37, 17], [38, 17],
                 [39, 17], [82, 17], [83, 17], [84, 17], [85, 17], [86, 17], [87, 17], [88, 17], [89, 17], [90, 17],
                 [91, 17], [28, 18], [29, 18], [30, 18], [31, 18], [32, 18], [33, 18], [34, 18], [35, 18], [36, 18],
                 [37, 18], [38, 18], [39, 18], [40, 18], [80, 18], [81, 18], [82, 18], [83, 18], [84, 18], [85, 18],
                 [86, 18], [87, 18], [88, 18], [89, 18], [90, 18], [91, 18], [92, 18], [27, 19], [28, 19], [29, 19],
                 [30, 19], [31, 19], [32, 19], [33, 19], [34, 19], [35, 19], [36, 19], [37, 19], [38, 19], [39, 19],
                 [40, 19], [41, 19], [79, 19], [80, 19], [81, 19], [82, 19], [83, 19], [84, 19], [85, 19], [86, 19],
                 [87, 19], [88, 19], [89, 19], [90, 19], [91, 19], [92, 19], [93, 19], [27, 20], [28, 20], [29, 20],
                 [30, 20], [31, 20], [32, 20], [33, 20], [34, 20], [35, 20], [36, 20], [37, 20], [38, 20], [39, 20],
                 [40, 20], [41, 20], [79, 20], [80, 20], [81, 20], [82, 20], [83, 20], [84, 20], [85, 20], [86, 20],
                 [87, 20], [88, 20], [89, 20], [90, 20], [91, 20], [92, 20], [93, 20], [27, 21], [28, 21], [29, 21],
                 [30, 21], [31, 21], [32, 21], [33, 21], [34, 21], [35, 21], [36, 21], [37, 21], [38, 21], [39, 21],
                 [40, 21], [41, 21], [79, 21], [80, 21], [81, 21], [82, 21], [83, 21], [84, 21], [85, 21], [86, 21],
                 [87, 21], [88, 21], [89, 21], [90, 21], [91, 21], [92, 21], [93, 21], [27, 22], [28, 22], [29, 22],
                 [30, 22], [31, 22], [32, 22], [33, 22], [34, 22], [35, 22], [36, 22], [37, 22], [38, 22], [39, 22],
                 [40, 22], [41, 22], [79, 22], [80, 22], [81, 22], [82, 22], [83, 22], [84, 22], [85, 22], [86, 22],
                 [87, 22], [88, 22], [89, 22], [90, 22], [91, 22], [92, 22], [93, 22], [28, 23], [29, 23], [30, 23],
                 [31, 23], [32, 23], [33, 23], [34, 23], [35, 23], [36, 23], [37, 23], [38, 23], [39, 23], [40, 23],
                 [80, 23], [81, 23], [82, 23], [83, 23], [84, 23], [85, 23], [86, 23], [87, 23], [88, 23], [89, 23],
                 [90, 23], [91, 23], [92, 23], [29, 24], [30, 24], [31, 24], [32, 24], [33, 24], [34, 24], [35, 24],
                 [36, 24], [37, 24], [38, 24], [39, 24], [82, 24], [83, 24], [84, 24], [85, 24], [86, 24], [87, 24],
                 [88, 24], [89, 24], [90, 24], [91, 24], [32, 25], [33, 25], [34, 25], [35, 25], [36, 25], [84, 25],
                 [85, 25], [86, 25], [87, 25], [88, 25], [43, 38], [44, 38], [45, 38], [77, 38], [78, 38], [79, 38],
                 [42, 39], [43, 39], [44, 39], [45, 39], [46, 39], [76, 39], [77, 39], [78, 39], [79, 39], [80, 39],
                 [42, 40], [43, 40], [44, 40], [45, 40], [46, 40], [47, 40], [48, 40], [75, 40], [76, 40], [77, 40],
                 [78, 40], [79, 40], [80, 40], [42, 41], [43, 41], [44, 41], [45, 41], [46, 41], [47, 41], [48, 41],
                 [49, 41], [74, 41], [75, 41], [76, 41], [77, 41], [78, 41], [79, 41], [80, 41], [43, 42], [44, 42],
                 [45, 42], [46, 42], [47, 42], [48, 42], [49, 42], [50, 42], [72, 42], [73, 42], [74, 42], [75, 42],
                 [76, 42], [77, 42], [78, 42], [79, 42], [44, 43], [45, 43], [46, 43], [47, 43], [48, 43], [49, 43],
                 [50, 43], [51, 43], [52, 43], [71, 43], [72, 43], [73, 43], [74, 43], [75, 43], [76, 43], [77, 43],
                 [78, 43], [46, 44], [47, 44], [48, 44], [49, 44], [50, 44], [51, 44], [52, 44], [53, 44], [70, 44],
                 [71, 44], [72, 44], [73, 44], [74, 44], [75, 44], [76, 44], [77, 44], [47, 45], [48, 45], [49, 45],
                 [50, 45], [51, 45], [52, 45], [53, 45], [54, 45], [69, 45], [70, 45], [71, 45], [72, 45], [73, 45],
                 [74, 45], [75, 45], [76, 45], [48, 46], [49, 46], [50, 46], [51, 46], [52, 46], [53, 46], [54, 46],
                 [55, 46], [56, 46], [67, 46], [68, 46], [69, 46], [70, 46], [71, 46], [72, 46], [73, 46], [74, 46],
                 [50, 47], [51, 47], [52, 47], [53, 47], [54, 47], [55, 47], [56, 47], [57, 47], [58, 47], [66, 47],
                 [67, 47], [68, 47], [69, 47], [70, 47], [71, 47], [72, 47], [73, 47], [51, 48], [52, 48], [53, 48],
                 [54, 48], [55, 48], [56, 48], [57, 48], [58, 48], [59, 48], [60, 48], [61, 48], [62, 48], [63, 48],
                 [64, 48], [65, 48], [66, 48], [67, 48], [68, 48], [69, 48], [70, 48], [71, 48], [72, 48], [52, 49],
                 [53, 49], [54, 49], [55, 49], [56, 49], [57, 49], [58, 49], [59, 49], [60, 49], [61, 49], [62, 49],
                 [63, 49], [64, 49], [65, 49], [66, 49], [67, 49], [68, 49], [69, 49], [70, 49], [71, 49], [54, 50],
                 [55, 50], [56, 50], [57, 50], [58, 50], [59, 50], [60, 50], [61, 50], [62, 50], [63, 50], [64, 50],
                 [65, 50], [66, 50], [67, 50], [68, 50], [69, 50], [56, 51], [57, 51], [58, 51], [59, 51], [60, 51],
                 [61, 51], [62, 51], [63, 51], [64, 51], [65, 51], [66, 51], [67, 51], [68, 51], [58, 52], [59, 52],
                 [60, 52], [61, 52], [62, 52], [63, 52], [64, 52], [65, 52], [66, 52]]
    TaoHuaFace = [[73, 10], [74, 10], [75, 10], [76, 10], [77, 10], [78, 10], [82, 10], [83, 10], [84, 10], [85, 10],
                  [86, 10], [87, 10], [30, 11], [31, 11], [32, 11], [33, 11], [34, 11], [35, 11], [38, 11], [39, 11],
                  [40, 11], [41, 11], [42, 11], [43, 11], [72, 11], [73, 11], [74, 11], [75, 11], [76, 11], [77, 11],
                  [78, 11], [79, 11], [80, 11], [81, 11], [82, 11], [83, 11], [84, 11], [85, 11], [86, 11], [87, 11],
                  [88, 11], [89, 11], [28, 12], [29, 12], [30, 12], [31, 12], [32, 12], [33, 12], [34, 12], [35, 12],
                  [36, 12], [37, 12], [38, 12], [39, 12], [40, 12], [41, 12], [42, 12], [43, 12], [44, 12], [45, 12],
                  [72, 12], [73, 12], [74, 12], [75, 12], [76, 12], [77, 12], [78, 12], [79, 12], [80, 12], [81, 12],
                  [82, 12], [83, 12], [84, 12], [85, 12], [86, 12], [87, 12], [88, 12], [89, 12], [28, 13], [29, 13],
                  [30, 13], [31, 13], [32, 13], [33, 13], [34, 13], [35, 13], [36, 13], [37, 13], [38, 13], [39, 13],
                  [40, 13], [41, 13], [42, 13], [43, 13], [44, 13], [45, 13], [71, 13], [72, 13], [73, 13], [74, 13],
                  [75, 13], [76, 13], [77, 13], [78, 13], [79, 13], [80, 13], [81, 13], [82, 13], [83, 13], [84, 13],
                  [85, 13], [86, 13], [87, 13], [88, 13], [89, 13], [90, 13], [91, 13], [28, 14], [29, 14], [30, 14],
                  [31, 14], [32, 14], [33, 14], [34, 14], [35, 14], [36, 14], [37, 14], [38, 14], [39, 14], [40, 14],
                  [41, 14], [42, 14], [43, 14], [44, 14], [45, 14], [71, 14], [72, 14], [73, 14], [74, 14], [75, 14],
                  [76, 14], [77, 14], [78, 14], [79, 14], [80, 14], [81, 14], [82, 14], [83, 14], [84, 14], [85, 14],
                  [86, 14], [87, 14], [88, 14], [89, 14], [90, 14], [91, 14], [27, 15], [28, 15], [29, 15], [30, 15],
                  [31, 15], [32, 15], [33, 15], [34, 15], [35, 15], [36, 15], [37, 15], [38, 15], [39, 15], [40, 15],
                  [41, 15], [42, 15], [43, 15], [44, 15], [45, 15], [46, 15], [71, 15], [72, 15], [73, 15], [74, 15],
                  [75, 15], [76, 15], [77, 15], [78, 15], [79, 15], [80, 15], [81, 15], [82, 15], [83, 15], [84, 15],
                  [85, 15], [86, 15], [87, 15], [88, 15], [89, 15], [90, 15], [91, 15], [27, 16], [28, 16], [29, 16],
                  [30, 16], [31, 16], [32, 16], [33, 16], [34, 16], [35, 16], [36, 16], [37, 16], [38, 16], [39, 16],
                  [40, 16], [41, 16], [42, 16], [43, 16], [44, 16], [45, 16], [46, 16], [71, 16], [72, 16], [73, 16],
                  [74, 16], [75, 16], [76, 16], [77, 16], [78, 16], [79, 16], [80, 16], [81, 16], [82, 16], [83, 16],
                  [84, 16], [85, 16], [86, 16], [87, 16], [88, 16], [89, 16], [90, 16], [91, 16], [27, 17], [28, 17],
                  [29, 17], [30, 17], [31, 17], [32, 17], [33, 17], [34, 17], [35, 17], [36, 17], [37, 17], [38, 17],
                  [39, 17], [40, 17], [41, 17], [42, 17], [43, 17], [44, 17], [45, 17], [46, 17], [72, 17], [73, 17],
                  [74, 17], [75, 17], [76, 17], [77, 17], [78, 17], [79, 17], [80, 17], [81, 17], [82, 17], [83, 17],
                  [84, 17], [85, 17], [86, 17], [87, 17], [88, 17], [89, 17], [28, 18], [29, 18], [30, 18], [31, 18],
                  [32, 18], [33, 18], [34, 18], [35, 18], [36, 18], [37, 18], [38, 18], [39, 18], [40, 18], [41, 18],
                  [42, 18], [43, 18], [44, 18], [45, 18], [72, 18], [73, 18], [74, 18], [75, 18], [76, 18], [77, 18],
                  [78, 18], [79, 18], [80, 18], [81, 18], [82, 18], [83, 18], [84, 18], [85, 18], [86, 18], [87, 18],
                  [88, 18], [89, 18], [28, 19], [29, 19], [30, 19], [31, 19], [32, 19], [33, 19], [34, 19], [35, 19],
                  [36, 19], [37, 19], [38, 19], [39, 19], [40, 19], [41, 19], [42, 19], [43, 19], [44, 19], [45, 19],
                  [72, 19], [73, 19], [74, 19], [75, 19], [76, 19], [77, 19], [78, 19], [79, 19], [80, 19], [81, 19],
                  [82, 19], [83, 19], [84, 19], [85, 19], [86, 19], [87, 19], [88, 19], [89, 19], [28, 20], [29, 20],
                  [30, 20], [31, 20], [32, 20], [33, 20], [34, 20], [35, 20], [36, 20], [37, 20], [38, 20], [39, 20],
                  [40, 20], [41, 20], [42, 20], [43, 20], [44, 20], [45, 20], [73, 20], [74, 20], [75, 20], [76, 20],
                  [77, 20], [78, 20], [79, 20], [80, 20], [81, 20], [82, 20], [83, 20], [84, 20], [85, 20], [86, 20],
                  [87, 20], [88, 20], [89, 20], [30, 21], [31, 21], [32, 21], [33, 21], [34, 21], [35, 21], [36, 21],
                  [37, 21], [38, 21], [39, 21], [40, 21], [41, 21], [42, 21], [43, 21], [44, 21], [45, 21], [73, 21],
                  [74, 21], [75, 21], [76, 21], [77, 21], [78, 21], [79, 21], [80, 21], [81, 21], [82, 21], [83, 21],
                  [84, 21], [85, 21], [86, 21], [87, 21], [30, 22], [31, 22], [32, 22], [33, 22], [34, 22], [35, 22],
                  [36, 22], [37, 22], [38, 22], [39, 22], [40, 22], [41, 22], [42, 22], [43, 22], [75, 22], [76, 22],
                  [77, 22], [78, 22], [79, 22], [80, 22], [81, 22], [82, 22], [83, 22], [84, 22], [85, 22], [86, 22],
                  [31, 23], [32, 23], [33, 23], [34, 23], [35, 23], [36, 23], [37, 23], [38, 23], [39, 23], [40, 23],
                  [41, 23], [42, 23], [75, 23], [76, 23], [77, 23], [78, 23], [79, 23], [80, 23], [81, 23], [82, 23],
                  [83, 23], [84, 23], [85, 23], [86, 23], [31, 24], [32, 24], [33, 24], [34, 24], [35, 24], [36, 24],
                  [37, 24], [38, 24], [39, 24], [40, 24], [41, 24], [42, 24], [77, 24], [78, 24], [79, 24], [80, 24],
                  [81, 24], [82, 24], [83, 24], [84, 24], [33, 25], [34, 25], [35, 25], [36, 25], [37, 25], [38, 25],
                  [39, 25], [40, 25], [78, 25], [79, 25], [80, 25], [81, 25], [82, 25], [83, 25], [34, 26], [35, 26],
                  [36, 26], [37, 26], [38, 26], [39, 26], [79, 26], [80, 26], [81, 26], [36, 27], [37, 27], [56, 29],
                  [57, 29], [58, 29], [59, 29], [60, 29], [53, 30], [54, 30], [55, 30], [56, 30], [57, 30], [58, 30],
                  [59, 30], [60, 30], [61, 30], [62, 30], [63, 30], [52, 31], [53, 31], [54, 31], [55, 31], [56, 31],
                  [57, 31], [58, 31], [59, 31], [60, 31], [61, 31], [62, 31], [63, 31], [64, 31], [51, 32], [52, 32],
                  [53, 32], [54, 32], [55, 32], [56, 32], [57, 32], [58, 32], [59, 32], [60, 32], [61, 32], [62, 32],
                  [63, 32], [64, 32], [65, 32], [50, 33], [51, 33], [52, 33], [53, 33], [54, 33], [55, 33], [56, 33],
                  [57, 33], [58, 33], [59, 33], [60, 33], [61, 33], [62, 33], [63, 33], [64, 33], [65, 33], [66, 33],
                  [49, 34], [50, 34], [51, 34], [52, 34], [53, 34], [54, 34], [55, 34], [61, 34], [62, 34], [63, 34],
                  [64, 34], [65, 34], [66, 34], [67, 34], [49, 35], [50, 35], [51, 35], [52, 35], [53, 35], [54, 35],
                  [62, 35], [63, 35], [64, 35], [65, 35], [66, 35], [67, 35], [49, 36], [50, 36], [51, 36], [52, 36],
                  [53, 36], [63, 36], [64, 36], [65, 36], [66, 36], [67, 36], [48, 37], [49, 37], [50, 37], [51, 37],
                  [52, 37], [64, 37], [65, 37], [66, 37], [67, 37], [68, 37], [48, 38], [49, 38], [50, 38], [51, 38],
                  [52, 38], [64, 38], [65, 38], [66, 38], [67, 38], [68, 38], [48, 39], [49, 39], [50, 39], [51, 39],
                  [52, 39], [64, 39], [65, 39], [66, 39], [67, 39], [68, 39], [48, 40], [49, 40], [50, 40], [51, 40],
                  [52, 40], [64, 40], [65, 40], [66, 40], [67, 40], [68, 40], [48, 41], [49, 41], [50, 41], [51, 41],
                  [52, 41], [64, 41], [65, 41], [66, 41], [67, 41], [68, 41], [49, 42], [50, 42], [51, 42], [52, 42],
                  [53, 42], [63, 42], [64, 42], [65, 42], [66, 42], [67, 42], [49, 43], [50, 43], [51, 43], [52, 43],
                  [53, 43], [54, 43], [62, 43], [63, 43], [64, 43], [65, 43], [66, 43], [67, 43], [49, 44], [50, 44],
                  [51, 44], [52, 44], [53, 44], [54, 44], [55, 44], [61, 44], [62, 44], [63, 44], [64, 44], [65, 44],
                  [66, 44], [67, 44], [50, 45], [51, 45], [52, 45], [53, 45], [54, 45], [55, 45], [56, 45], [57, 45],
                  [58, 45], [59, 45], [60, 45], [61, 45], [62, 45], [63, 45], [64, 45], [65, 45], [66, 45], [51, 46],
                  [52, 46], [53, 46], [54, 46], [55, 46], [56, 46], [57, 46], [58, 46], [59, 46], [60, 46], [61, 46],
                  [62, 46], [63, 46], [64, 46], [65, 46], [52, 47], [53, 47], [54, 47], [55, 47], [56, 47], [57, 47],
                  [58, 47], [59, 47], [60, 47], [61, 47], [62, 47], [63, 47], [64, 47], [53, 48], [54, 48], [55, 48],
                  [56, 48], [57, 48], [58, 48], [59, 48], [60, 48], [61, 48], [62, 48], [63, 48], [56, 49], [57, 49],
                  [58, 49], [59, 49], [60, 49]]


class ESPMqtt:
    def __init__(self, broker, port, client_id="esp32_client"):
        self._mqtt_client = None
        self._broker = broker
        self._port = port
        self._client_id = client_id

    def connect(self, callback=None):
        try:
            self._mqtt_client = MQTTClient(self._client_id, self._broker, self._port)
            self._mqtt_client.connect()
            if callback:
                self._mqtt_client.set_callback(callback)
            print('MQTT连接成功')
            return True
        except Exception as e:
            print(f"MQTT连接失败: {e}")
            return False

    def publish(self, topic, message):
        """发布消息"""
        try:
            if isinstance(message, dict):
                message = json.dumps(message)
            return self._mqtt_client.publish(topic, message)
        except Exception as e:
            print(f"发布消息失败: {e}")
            return False


class ESPHotWifi:
    # 将HTML模板移到类的常量中
    HTML_TEMPLATE = """
        <html>
        <head>
        <title>simple server</title>
        </head>
        <body>
        <p>{content}</p>
        </body>
        </html>
    """

    HTTP_HEADER = "HTTP/1.1 {status}\r\nServer: simple server\r\n" \
                  "Content-Type: text/html; charset=utf-8\r\n" \
                  "Content-Length: {length}\r\nConnection: close\r\n\r\n"

    def __init__(self, host: str = '127.0.0.1', port: int = 80):
        self.host = host
        self.port = port
        self.server = self._create_server()

    def _create_server(self) -> socket.socket:
        """创建并配置服务器socket"""
        server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 5)
        server.bind((self.host, self.port))
        server.listen(5)
        return server

    def get_path(self, data: str) -> str:
        """解析HTTP请求中的路径"""
        try:
            first_line = data.split('\r\n')[0]
            return first_line.split()[1]
        except (IndexError, AttributeError):
            return ""

    def get_payload(self, url_path: str):
        """解析URL中的参数"""
        try:
            query_string = url_path.split("?")[-1]
            return dict(item.split('=') for item in query_string.split('&'))
        except (IndexError, ValueError):
            return {}

    def get_html(self, status: str, content: str) -> str:
        """生成HTTP响应"""
        html = self.HTML_TEMPLATE.format(content=content)
        length = len(html.encode())
        head = self.HTTP_HEADER.format(status=status, length=length)
        return head + html

    def get_404(self) -> str:
        return self.get_html('404 NOT FOUND', "你访问的资源不存在")

    def modify(self, path: str) -> str:
        """处理配置修改请求"""
        payload = self.get_payload(path)
        config_data = self._read_config()


        config_data['wifi']['ssid'] = payload['WifiSSID']
        config_data['wifi']['password'] = payload['WifiPassword']
        config_data['mqtt']['broker'] = payload['MqttIP']
        config_data['pins']['scl'] = payload['PinSCL']
        config_data['pins']['sda'] = payload['PinSDA']
        config_data['pins']['ptz_head'] = payload['PinPTZHead']
        config_data['pins']['ptz_body'] = payload['PinPTZBody']
        config_data['pins']['led'] = payload['PinLED']

        self._write_config(config_data)
        return self.get_html("200 OK", "配置已经修改完毕正在重启")

    def _read_config(self) -> dict:
        """读取配置文件"""
        with open('./config.toml', 'r') as f:
            # return toml.load(f)
            return utoml.load(f)

    def _write_config(self, config_data: dict) -> None:
        """写入配置文件"""
        with open('config.toml', 'w') as f:
            utoml.dump(config_data, f)
            # toml.dump(config_data, f)

    def get_index(self) -> str:
        """处理首页请求"""
        config_data = self._read_config()

        with open('config.html', 'r', encoding='utf-8') as f:
            html_str = f.read()

        # 使用更安全的格式化方式
        replacements = {
            '{DeviceID}': str(config_data['device']['id']),
            '{WifiSSID}': str(config_data['wifi']['ssid']),
            '{WifiPassword}': str(config_data['wifi']['password']),
            '{MqttIP}': str(config_data['mqtt']['broker']),
            '{SoftVersion}': str(config_data['device']['version']),
            '{PinSCL}': str(config_data['pins']['scl']),
            '{PinSDA}': str(config_data['pins']['sda']),
            '{PinPTZHead}': str(config_data['pins']['ptz_head']),
            '{PinPTZBody}': str(config_data['pins']['ptz_body']),
            '{PinLED}': str(config_data['pins']['led'])
        }

        for key, value in replacements.items():
            html_str = html_str.replace(key, value)

        return self.get_html("200 OK", html_str)

    def get_html_by_path(self, path: str) -> str:
        """根据路径返回相应的页面"""
        route_handlers = {
            '/': self.get_index,
            '/modify': lambda: self.modify(path)
        }

        handler = route_handlers.get(path.split('?')[0], self.get_404)
        return handler()

    def start(self) -> None:
        """启动服务器"""
        while True:
            client, address = self.server.accept()

            data = client.recv(1024).decode()
            path = self.get_path(data)
            response = self.get_html_by_path(path)

            client.send(response.encode())

            if '配置已经修改完毕正在重启' in response:
                self.server.close()
                break



class ESPConfig:
    def _file_exists(self, file_path):
        try:
            os.stat(file_path)
            return True
        except OSError as e:
            if e.errno == 2:  # OSError的错误码2表示文件不存在
                return False
            raise e

    def init_config_file(self):
        """初始化配置文件"""
        config_path = './config.toml'
        if not self._file_exists(config_path):
            machine_id = binascii.hexlify(machine.unique_id())
            config_dict = {
                'device': {
                    'id': machine_id,
                    'version': '1.0.0'
                },
                'wifi': {
                    'ssid': '',
                    'password': '',
                    'ap_ssid': 'esp_config',
                    'ap_password': '12345678',
                    'connect_timeout': 30
                },
                'mqtt': {
                    'broker': '39.104.71.213',
                    'port': 1883,
                    'topic': f'esp32/server/{machine_id}'
                },
                'pins': {
                    'scl': 21,
                    'sda': 22,
                    'ptz_head': 13,
                    'ptz_body': 14,
                    'led': 2
                }
            }

            with open(config_path, 'w') as f:
                utoml.dump(config_dict, f)

            return config_dict
        else:
            with open(config_path, 'r') as f:
                return utoml.load(f)


class ESPController:
    """ESP32主控制器类"""

    def __init__(self, config):
        # 初始化板载led，通过板载led去展示裸板运行出现情况
        self.led = Pin(2, Pin.OUT)
        self._start_led_blink()

        self.config = config
        # 初始化屏幕
        self.screen = ESPScreenControl(
            self.config['pins']['scl'],
            self.config['pins']['sda']
        )

        # 初始化云台
        self.ptz = ESPTPZControl(
            self.config['pins']['ptz_head'],
            self.config['pins']['ptz_body']
        )

        # MQTT客户端
        self.mqtt = ESPMqtt(
                self.config['mqtt']['broker'],
                self.config['mqtt']['port']
            )

        # WiFi客户端
        self.wifi = network.WLAN(network.STA_IF)
    def _start_led_blink(self):
        """启动LED闪烁，表示系统正在初始化"""
        self.led_timer = Timer(-1)
        self.led_timer.init(period=300, mode=Timer.PERIODIC,
                          callback=lambda t: self.led.value(not self.led.value()))
    def _stop_led_blink(self):
        """停止LED闪烁，常亮表示系统正常运行"""
        if hasattr(self, 'led_timer'):
            self.led_timer.deinit()
        self.led.value(1)  # LED常亮

    def _error_led_blink(self):
        """系统异常时LED快速闪烁"""
        if hasattr(self, 'led_timer'):
            self.led_timer.deinit()
        self.led_timer.init(period=50, mode=Timer.PERIODIC,
                            callback=lambda t: self.led.value(not self.led.value()))


    def connect_wifi(self):
        """连接WiFi"""
        try:
            self.wifi.active(True)
            if not self.wifi.isconnected():
                self.screen.show_message_to_screen("正在连接WiFi...")
                self.wifi.connect(
                    self.config['wifi']['ssid'],
                    self.config['wifi']['password']
                )

                start_time = time.time()
                while not self.wifi.isconnected():
                    self.led.value(not self.led.value())  # LED闪烁
                    time.sleep(0.3)

                    if time.time() - start_time > 30:
                        raise TimeoutError("WiFi连接超时")

            self.screen.show_message_to_screen("WiFi连接成功!")
            self._stop_led_blink()
            return True

        except Exception as e:
            self.screen.show_message_to_screen(f"WiFi连接失败: {str(e)}")
            return False

    def _on_mqtt_message(self, topic, msg):
        """MQTT消息处理"""
        try:
            message = msg.decode()
            topic = topic.decode()
            print(f"收到消息: {topic} -> {message}")

            # 这里可以添加消息处理逻辑
            self.screen.show_message_to_screen(message)

        except Exception as e:
            print(f"处理MQTT消息失败: {e}")

    def connect_mqtt(self):
        """连接MQTT服务器"""
        if not self.mqtt.connect(self._on_mqtt_message):
            raise Exception("MQTT连接失败")

        # 订阅主题
        self.mqtt._mqtt_client.subscribe(self.config['mqtt']['topic'])
        self.screen.show_message_to_screen("MQTT连接成功")

        # 发送上线消息
        self.mqtt.publish(
            f"esp32/client/{self.config['device']['id']}/status",
            {"status": "online"}
        )

        return True

    def start(self):
        """启动设备"""
        self.screen.show_message_to_screen("设备启动中...")
        time.sleep(2)

        self.screen.show_message_to_screen("连接wifi中...")
        if self.connect_wifi():
            self.screen.show_message_to_screen("连接mqtt中...")
            if self.connect_mqtt():
                self.screen.show_message_to_screen("MQTT连接成功")
            else:
                self.screen.show_message_to_screen("MQTT连接失败")
        else:
            # 开启热点，自己配置wifi
            esp_host = ESPHotWifi(host='192.168.4.1')
            esp_host.start()
            del esp_host
            return

        self.screen.show_picture_to_screen(ESPPicture.HappyFace)
        time.sleep(1)

if __name__ == "__main__":
    esp_config = ESPConfig()
    config = esp_config.init_config_file()

    esp = ESPController(config)
    esp.start()
